package io.github.harmonly.controller.api;

import io.github.harmonly.entity.activity.Participant;
import io.github.harmonly.service.ParticipantService;
import io.github.harmonly.util.JSONUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/api")
public class ParticipantController {

    @Resource
    private ParticipantService service;

    @ResponseBody
    @RequestMapping(value = "/participants_by_uid/{uid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getAssociations(@PathVariable("uid") int uid) {
        return JSONUtil.toJSONString(service.getParticipantByUser(uid));
    }

    @ResponseBody
    @RequestMapping(value = "/participants_by_cid/{cid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getUsers(@PathVariable("cid") int cid) {
        return JSONUtil.toJSONString(service.getParticipantByActivity(cid));
    }

    @ResponseBody
    @RequestMapping(value = "/participant", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addParticipant(@RequestBody String json) {
        return JSONUtil.result(service.addParticipant(JSONUtil.parseObject(json, Participant.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/participant", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteParticipant(@RequestBody String json) {
        return JSONUtil.result(service.deleteParticipant(JSONUtil.parseObject(json, Participant.class)));
    }
}
